﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Default : System.Web.UI.Page
{
    NguoiDungManager nguoiDungManager;

    protected void Page_Load(object sender, EventArgs e)
    {
        nguoiDungManager = new NguoiDungManager();

        if (!IsPostBack)
        {
            TxtDateStart.Text = DateTime.Now.ToString("dd/MM/yyyy");
            TxtDateEnd.Text = DateTime.Now.ToString("dd/MM/yyyy");
            SearchData();
        }
    }

    private void SearchData()
    {
        NguoiDung nguoiDung = new NguoiDung();
        nguoiDung.Username = TxtUsername.Text.Trim();
        nguoiDung.Fullname = TxtName.Text.Trim();
        nguoiDung.DienThoai = TxtPhone.Text.Trim();
        nguoiDung.Email = TxtEmail.Text.Trim();
        if (TxtDateStart.Text.Trim() != "")
        {
            nguoiDung.NgayDK = DateTime.ParseExact(TxtDateStart.Text.Trim() + " 00:00:00", "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
        }

        DateTime? NgayEnd = null;
        if (TxtDateEnd.Text.Trim() != "")
        {
            NgayEnd = DateTime.ParseExact(TxtDateEnd.Text.Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
        }
        gvNguoiDung.DataSource = nguoiDungManager.AdminSearchNguoiDung(nguoiDung, NgayEnd);
        gvNguoiDung.DataBind();
    }

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        SearchData();
    }
    protected void gvNguoiDung_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string username = gvNguoiDung.DataKeys[e.RowIndex].Value.ToString();
        NguoiDung nguoiDung = new NguoiDung();
        nguoiDung.Username = username;
        if (nguoiDungManager.DeleteNguoiDung(nguoiDung))
        {
            CommonFuns.ShowPopUpMsg(this, "Xóa thông tin thành công.");
            SearchData();
        }
        else
            CommonFuns.ShowPopUpMsg(this, "Xóa thông tin thất bại");

    }
    protected void gvNguoiDung_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        NguoiDung nguoiDung = new NguoiDung();
        nguoiDung.Username = gvNguoiDung.DataKeys[e.RowIndex].Value.ToString();
        nguoiDung.Fullname = (gvNguoiDung.Rows[e.RowIndex].Cells[1].Controls[1] as TextBox).Text;
        nguoiDung.Lock = (gvNguoiDung.Rows[e.RowIndex].Cells[5].Controls[0] as CheckBox).Checked;
        if (nguoiDungManager.UpdateNguoiDungAdmin(nguoiDung))
        {
            CommonFuns.ShowPopUpMsg(this, "Cập nhật thông tin thành công.");
            gvNguoiDung.EditIndex = -1;
            SearchData();
        }
        else
            CommonFuns.ShowPopUpMsg(this, "Cập nhật thông tin không thành công.");
    }
    protected void gvNguoiDung_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvNguoiDung.EditIndex = e.NewEditIndex;
        SearchData();
    }
    protected void gvNguoiDung_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvNguoiDung.EditIndex = -1;
        SearchData();
    }
}